//
// Created by denglibin on 2021/5/31.
//

#include "tetris_spirit.h"
#include "malloc.h"
#include "stdlib.h"
//将二维数组通过指针旋转90度(顺时针转90度)
static void rotate(int buf[4][4],int n)
{
    int i,j,t;
    int *parr; //指向int类型的指针

    parr=&buf[0][0]; //将二维数组看成一维数组

    for(i=0;i<=n-1;i++)
    {
        for(j=i;j<n-i-1;j++)
        {
            t=*(parr+i*n+j);
            *(parr+i*n+j)=*(parr+(n-1-j)*n+i);
            *(parr+(n-1-j)*n+i)=*(parr+(n-1-i)*n+n-1-j);
            *(parr+(n-1-i)*n+n-1-j)=*(parr+j*n+n-1-i);
            *(parr+j*n+n-1-i)=t;
        }
    }
}

static void setToZero(TetrisSpirit* spirit){
    for(int i = 0; i < 4; i++){
        for(int j = 0; j < 4; j++){
            spirit->flags[i][j] = 0;
        }
    }
}
static void setToO(TetrisSpirit* tetrisSpirit){
    tetrisSpirit->type = O;
    tetrisSpirit->flags[1][1] = 1;
    tetrisSpirit->flags[1][2] = 1;
    tetrisSpirit->flags[2][1] = 1;
    tetrisSpirit->flags[2][2] = 1;
}
static void setToI(TetrisSpirit* tetrisSpirit){
    tetrisSpirit->type = I;
    tetrisSpirit->flags[0][1] = 1;
    tetrisSpirit->flags[1][1] = 1;
    tetrisSpirit->flags[2][1] = 1;
    tetrisSpirit->flags[3][1] = 1;
}
static void setToS(TetrisSpirit* tetrisSpirit){
    tetrisSpirit->type = S;
    tetrisSpirit->flags[1][1] = 1;
    tetrisSpirit->flags[1][2] = 1;
    tetrisSpirit->flags[2][0] = 1;
    tetrisSpirit->flags[2][1] = 1;
}
static void setToZ(TetrisSpirit* tetrisSpirit){
    tetrisSpirit->type = Z;
    tetrisSpirit->flags[1][0] = 1;
    tetrisSpirit->flags[1][1] = 1;
    tetrisSpirit->flags[2][1] = 1;
    tetrisSpirit->flags[2][2] = 1;
}
static void setToL(TetrisSpirit* tetrisSpirit){
    tetrisSpirit->type = L;
    tetrisSpirit->flags[0][1] = 1;
    tetrisSpirit->flags[1][1] = 1;
    tetrisSpirit->flags[2][1] = 1;
    tetrisSpirit->flags[2][2] = 1;
}

static void setToJ(TetrisSpirit* tetrisSpirit){
    tetrisSpirit->type = J;
    tetrisSpirit->flags[0][2] = 1;
    tetrisSpirit->flags[1][2] = 1;
    tetrisSpirit->flags[2][2] = 1;
    tetrisSpirit->flags[2][1] = 1;
}

static void setToT(TetrisSpirit* tetrisSpirit){
    tetrisSpirit->type = T;
    tetrisSpirit->flags[1][0] = 1;
    tetrisSpirit->flags[1][1] = 1;
    tetrisSpirit->flags[1][2] = 1;
    tetrisSpirit->flags[2][1] = 1;
}



/**
 * 创建一个随机类型的形状
 * @return
 */
TetrisSpirit* TetrisSpiritCreteRandomType(){
    TetrisSpirit* spirit = (TetrisSpirit*)malloc( sizeof (TetrisSpirit));
    spirit->x = 0;
    spirit->y = 0;
    spirit->x_direction = -1;
    int type = rand()%7 + 1;
    TetrisSpiritTransformTo(spirit, type);
    return spirit;
}


/**
 * 类型转换
 * @param tetrisSpirit
 * @param type
 */
void TetrisSpiritTransformTo(TetrisSpirit* tetrisSpirit, enum ShapeType type){
    setToZero(tetrisSpirit);
    switch (type) {
        case O:
            setToO(tetrisSpirit);
            break;
        case I:
            setToI(tetrisSpirit);
            break;
        case S:
            setToS(tetrisSpirit);
            break;
        case Z:
            setToZ(tetrisSpirit);
            break;
        case L:
            setToL(tetrisSpirit);
            break;
        case J:
            setToJ(tetrisSpirit);
            break;
        case T:
            setToT(tetrisSpirit);
            break;
        default:
            setToO(tetrisSpirit);

    }
}

/**
 * 顺时针旋转90度
 * @param tetrisSpirit
 */
void TetrisSpiritRotate(TetrisSpirit* tetrisSpirit){
   rotate(tetrisSpirit->flags, 4);
}
/**
 * 释放
 * @param tetrisSpirit
 */
void TetrisSpiritFree(TetrisSpirit* tetrisSpirit){
    free(tetrisSpirit);
}